package edu.neusoft.android.yuejian14.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.javabean.User;
import edu.neusoft.android.yuejian14.model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginActivity extends AppCompatActivity{
    private Button btn_login;
    private EditText username,password;
    private TextView btn_reg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.userpassword);
        btn_reg =(TextView)findViewById(R.id.btn_register);
        btn_reg.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);

            }
        });

        username.setText("user");
        password.setText("11");

        btn_login = (Button)findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                UserModel userModel = new UserModel();
                Call call = UserModel.login(username.getText().toString(),password.getText().toString());

                Callback<User> callback = new Callback<User>() {

                    public void onResponse(Call<User> call, Response<User> response) {
                        User userbean = response.body();
                        if (!"0".equals(userbean.getUid())){
                            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                            saveUser(userbean.getUid());
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                        else
                            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();

                    }
                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                        Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                    }
                };
                call.enqueue(callback);
            }
        });

    }


    protected void onActivityResult(int requestCode,int resultCode ,Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        if (requestCode==1&&resultCode==RESULT_OK){
            String et_username = data.getStringExtra("username");
            username.setText(et_username);
        }

    }

    public void saveUser(int uid) {
        SharedPreferences sharePreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences .Editor editor = sharePreferences.edit();//获取编译器
        editor.putString("username",username.getText().toString());
//        uid = Integer.parseInt(uid);
        editor.putInt("uid",uid);
        editor.putString("userpassword",password.getText().toString());
        editor.commit();
    }
}

